<%--
    Document   : doregister
    Author     : Mike
--%>
<%@page import="MailinglistPackage.MailingListService"%>
<%@page import="MailinglistPackage.MailingList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="register" class="login.RegisterInfo" scope="session"/>
<jsp:setProperty name="register" property="*"/>
<jsp:useBean id="member" class="login.userDBDriver" scope="session"/>

<%!
public String validateInputs(String inputusername, String inputemail, String inputrealname, String inputpassword, String inputpasswordconfirm, boolean inputexist)
{
    String errormessage = "none";
    try{
            if(inputusername.equals("") || inputemail.equals("") || inputrealname.equals("") || inputpassword.equals("") || inputusername == null || inputemail == null || inputrealname == null || inputpassword == null)
            {
                errormessage = "Registration failed. Cant be blank.";
            }
            else if(inputexist == true)
            {
                errormessage = "Registration failed. Username already exists.";
            }
        }
    catch (NullPointerException e)
    {
        errormessage = "Registration failed. Cant be blank.";
    }
    return errormessage;
}
%>

<%
    String statusMessage = "User registration successful.";
    int answercat = member.createCategoryDB("userDB");
    int answer1 = member.databaseConnect("userDB");
    String result1 = "";
    String result2 = "";
    boolean error = false;
    boolean existcheck = true;
    String tempUsername = "";
    String tempPassword = "";
    String tempPasswordConfirm = "";
    String tempEmail = "";
    String tempRealname = "";
    String redirectURL = "register.jsp";

    try{
        tempUsername = register.getUsername();
        tempPassword = register.getPassword();
        tempEmail = register.getEmail();
        tempRealname = register.getRealname();
        tempPasswordConfirm = register.getPasswordConfirm();
    }
    catch (NullPointerException e){
        error = true;
        statusMessage = "Registration failed. Cant be blank.";
    }

    //------------------------------------------------------------------------
    //Add user to mailing list service here!!!
    //------------------------------------------------------------------------

    try{
        existcheck = member.checkExists(tempUsername);
        result1 = validateInputs( tempUsername, tempEmail, tempRealname, tempPassword, tempPasswordConfirm, existcheck);
        if(result1.equals("none"))
        {
            result2 = member.addUser(tempUsername, tempRealname, tempEmail, tempPassword);
            //Add a user to the mailing list
            new MailingListService().getMailingListPort().addEmail(tempEmail, "Group9");

            if (! result2.equals("User saved")){
                error = true;
                statusMessage = "Registration failed. Try a different username.";
            }
            else{
                error = false;
                statusMessage = "User registration successful.";
            }
        }
        else
        {
           error = true;
           statusMessage = "Registration failed. Try a different username.";
        }
    }
    catch (NullPointerException e)
    {
        error = true;
        statusMessage = "Registration failed. Cant be blank.";
    }

//redirect to registration page
    session.setAttribute( "statusMessage", statusMessage);
    register.setAllNull();
    statusMessage = "";
    result1 = "";
    result2 = "";
    error = false;
    existcheck = true;
    tempUsername = "";
    tempPassword = "";
    tempPasswordConfirm = "";
    tempEmail = "";
    tempRealname = "";
    response.sendRedirect(redirectURL);
%>

